package com.ruoyi.project.tool.swagger;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.ehcache.EhcacheCache;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
@Slf4j
public class TestController extends BaseController {
    @Autowired
    private EhcacheCache ehcacheCache;

    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();

    {
        users.put(1, new UserEntity(1, "admin" , "admin123" , "15888888888"));
        users.put(2, new UserEntity(2, "ry" , "admin123" , "15666666666"));
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public AjaxResult userList() {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        return AjaxResult.success(userList);
    }

    @ApiOperation("根据KEY获取缓存")
    @GetMapping("/getCacheByKey")
    public AjaxResult getCacheByKey(String key) {
        log.debug("{}",JSON.toJSONString(ehcacheCache.getCacheObject(key)));
        return AjaxResult.success(JSON.toJSONString(ehcacheCache.getCacheObject(key)));
    }
    @ApiOperation("获取缓存列表")
    @GetMapping("/listCache")
    public AjaxResult listCache() {
        log.debug("{}\n", JSON.toJSONString(ehcacheCache.keys("sys")));
        log.debug("{}\n", JSON.toJSONString(ehcacheCache.keys()));
        Collection<String> keys = ehcacheCache.keys(Constants.RATE_LIMIT_KEY);
        keys.forEach(key -> {
            log.debug(Constants.RATE_LIMIT_KEY+"{}: \n",key);
        });
        return AjaxResult.success(ehcacheCache.keys());
    }

    @ApiOperation("清空缓存")
    @ApiImplicitParam(name = "key" , value = "key" , required = false, dataType = "String")
    @GetMapping("/deleteObject")
    public AjaxResult deleteObject(String key) {
        if(StringUtils.isEmpty(key)){
            ehcacheCache.deleteObject();
        }else{
            ehcacheCache.deleteObject(key);
        }
        return AjaxResult.success(ehcacheCache.getSize());
    }

    @ApiOperation("检查key是否过期")
    @ApiImplicitParam(name = "key" , value = "key" , required = false, dataType = "String")
    @GetMapping("/isExpired")
    public AjaxResult isExpired(String key) {
        return AjaxResult.success(ehcacheCache.isExpired(key));
    }

    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "userId" , value = "用户ID" , required = true, dataType = "int" , paramType = "path")
    @GetMapping("/{userId}")
    public AjaxResult getUser(@PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            return AjaxResult.success(users.get(userId));
        } else {
            return error("用户不存在");
        }
    }

    @ApiOperation("新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId" , value = "用户id" , dataType = "Integer"),
            @ApiImplicitParam(name = "username" , value = "用户名称" , dataType = "String"),
            @ApiImplicitParam(name = "password" , value = "用户密码" , dataType = "String"),
            @ApiImplicitParam(name = "mobile" , value = "用户手机" , dataType = "String")
    })
    @PostMapping("/save")
    public AjaxResult save(UserEntity user) {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
            return error("用户ID不能为空");
        }
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("更新用户")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody UserEntity user) {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
            return error("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId())) {
            return error("用户不存在");
        }
        users.remove(user.getUserId());
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId" , value = "用户ID" , required = true, dataType = "int" , paramType = "path")
    @DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            users.remove(userId);
            return success();
        } else {
            return error("用户不存在");
        }
    }
}

@ApiModel(value = "UserEntity" , description = "用户实体")
class UserEntity {
    @ApiModelProperty("用户ID")
    private Integer userId;

    @ApiModelProperty("用户名称")
    private String username;

    @ApiModelProperty("用户密码")
    private String password;

    @ApiModelProperty("用户手机")
    private String mobile;

    public UserEntity() {

    }

    public UserEntity(Integer userId, String username, String password, String mobile) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.mobile = mobile;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}
